/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.biz.service.impl;

import me.zhengjie.modules.biz.domain.Booking;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.biz.repository.BookingRepository;
import me.zhengjie.modules.biz.service.BookingService;
import me.zhengjie.modules.biz.service.dto.BookingDto;
import me.zhengjie.modules.biz.service.dto.BookingQueryCriteria;
import me.zhengjie.modules.biz.service.mapstruct.BookingMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://eladmin.vip
* @description 服务实现
* @author Admin
* @date 2023-02-17
**/
@Service
@RequiredArgsConstructor
public class BookingServiceImpl implements BookingService {

    private final BookingRepository bookingRepository;
    private final BookingMapper bookingMapper;

    @Override
    public Map<String,Object> queryAll(BookingQueryCriteria criteria, Pageable pageable){
        Page<Booking> page = bookingRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(bookingMapper::toDto));
    }

    @Override
    public List<BookingDto> queryAll(BookingQueryCriteria criteria){
        return bookingMapper.toDto(bookingRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public BookingDto findById(Integer id) {
        Booking booking = bookingRepository.findById(id).orElseGet(Booking::new);
        ValidationUtil.isNull(booking.getId(),"Booking","id",id);
        return bookingMapper.toDto(booking);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public BookingDto create(Booking resources) {
        return bookingMapper.toDto(bookingRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(Booking resources) {
        Booking booking = bookingRepository.findById(resources.getId()).orElseGet(Booking::new);
        ValidationUtil.isNull( booking.getId(),"Booking","id",resources.getId());
        booking.copy(resources);
        bookingRepository.save(booking);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            bookingRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<BookingDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (BookingDto booking : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("类型", booking.getType());
            map.put("实验室", booking.getLabId());
            map.put("状态", booking.getStatus());
            map.put("审核人员", booking.getAuditorId());
            map.put("预约开始时点", booking.getStartAt());
            map.put("预约结束时间", booking.getEndAt());
            map.put("实验室管理员审核", booking.getAuditorStatus());
            map.put("实验室使用记录", booking.getLabUseRecordId());
            map.put("管理员审核备注", booking.getReason());
            map.put("预约人", booking.getCreatorId());
            map.put(" createdAt",  booking.getCreatedAt());
            map.put(" updatedAt",  booking.getUpdatedAt());
            map.put("是否删除", booking.getIsDeleted());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}